package com.sx.controller;

import java.util.List;
import java.util.UUID;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.bean.Role;
import com.sx.bean.Node;
import com.sx.bean.PtRRoleOrgan;
import com.sx.service.RoleService;
import com.sx.util.Pagination;


@Controller
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	

	public RoleService getRoleservice() {
		return roleService;
	}

	public void setRoleservice(RoleService roleservice) {
		this.roleService = roleservice;
	}
	 
	@RequestMapping("/role/Roles")
	@ResponseBody
	public List<Role>  selectRoleAll(){
		System.out.println("1111111111111111111111111111111");
		List<Role>  list= roleService.selectAll();
		System.out.println(list);
		return list;
	} 
	
	@RequestMapping("/duty/insert")
	@ResponseBody
	public String  insert(PtRRoleOrgan ptroleorgan){
		UUID uuid = UUID.randomUUID();
		ptroleorgan.setDutyid(uuid.toString().replace("-", ""));
		roleService.insert(ptroleorgan);
		return "1";
	} 
	

	
	@RequestMapping("/role/getRoles")
	@ResponseBody
	public Pagination<Role> getRoles(int pageNumber, int pageSize){	
		Pagination<Role> page = roleService.getRoles(pageNumber, pageSize);
		return page;
	}

	@RequestMapping("/role/insert")
	@ResponseBody
	public int InsertRole(String role_id, String role_name) {
		UUID uuid = UUID.randomUUID();
		String role_uuid = uuid.toString().replace("-", "");
		int data = 0;
		if (role_id.equals("") || role_name.equals("")) {
			return data;
		} else {
			roleService.insertRole(role_uuid, role_id, role_name);
			data = 1;
			return data;
		}
	}

	@RequestMapping("/roleRes/getResourcesByParentId")
	@ResponseBody
	public List<Node> GetResourcesByParentId(String parentId) {
		List<Node> list = roleService.getByParentId(parentId);

		return list;

	}

	@RequestMapping("/role/getResources")
	@ResponseBody
	public Role GetResourcesById(String roleId) {
		Role role = roleService.selectRoleByUUid(roleId);
		return role;

	}

	@RequestMapping("/role/linkResource")
	@ResponseBody
	public int AddResources(String roleId,String resource ) {
		System.out.println(roleId);
		String[] str = resource.split(",");
		for (String s : str) {
			System.out.println(s);
		}
		int data=roleService.insertRoleRes(roleId, str);
		return data;
	}

	/*
	 * @RequestMapping("/role/getRoles")
	 * 
	 * @ResponseBody public List<Role> GetRoles(String pageNumber, String pageSize)
	 * { System.out.println("1111"); List<Role> list = roleService.selectAll();
	 * System.out.println(list); return list; }
	 */

	@RequestMapping("/role/getRole")
	@ResponseBody
	public Role GetByRoleUUid(String uuid) {
		Role role = roleService.selectRoleByUUid(uuid);
		System.out.println(role);
		return role;
	}

	@RequestMapping("/role/update")
	@ResponseBody
	public int UpdateRole(String role_uuid, String role_id, String role_name) {
		int data = 0;
		if (role_id.equals("") || role_name.equals("")) {
			return data;
		} else {
			roleService.updateRoleByUUid(role_uuid, role_id, role_name);
			data = 1;
			return data;
		}
	}

	@RequestMapping("/role/remove")
	@ResponseBody
	public int DeleteRole(String id) {
		roleService.deleteRoleByUUid(id);
		return 1;
	}


}
